package com.nbsaas.boot.system.rest.resource;

import com.nbsaas.boot.system.api.apis.MenuApi;
import com.nbsaas.boot.system.data.entity.Menu;
import com.nbsaas.boot.system.api.domain.request.MenuRequest;
import com.nbsaas.boot.system.api.domain.response.MenuResponse;
import com.nbsaas.boot.system.api.domain.simple.MenuSimple;
import com.nbsaas.boot.system.rest.convert.MenuSimpleConvert;
import com.nbsaas.boot.system.rest.convert.MenuEntityConvert;
import com.nbsaas.boot.system.rest.convert.MenuResponseConvert;
import com.nbsaas.boot.system.data.repository.MenuRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.system.api.domain.request.MenuSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   菜单-业务接口实现
*/
@Transactional
@Service
public class MenuResource extends BaseResource<Menu,MenuResponse, MenuSimple, MenuRequest>  implements MenuApi {

    @Resource
    private MenuRepository menuRepository;

    @Override
    public JpaRepositoryImplementation<Menu, Serializable> getJpaRepository() {
        return menuRepository;
    }

    @Override
    public Function<Menu, MenuSimple> getConvertSimple() {
        return new MenuSimpleConvert();
    }

    @Override
    public Function<MenuRequest, Menu> getConvertForm() {
        return new MenuEntityConvert();
    }

    @Override
    public Function<Menu, MenuResponse> getConvertResponse() {
        return new MenuResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<MenuSimple> list(PageRequest request) {
        MenuSimpleConvert convert=new MenuSimpleConvert();
        if (request instanceof MenuSearch){
             MenuSearch searchRequest=(MenuSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<MenuSimple> root(MenuSearch search) {
        ListResponse<MenuSimple> result=new  ListResponse<>();
        List<MenuSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


